package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.RoleParam;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.PermissionVO;
import com.cskaoyan.bean.vo.SystemRoleVo;
import com.cskaoyan.mapper.MallAdminMapper;
import com.cskaoyan.mapper.MallPermissionMapper;
import com.cskaoyan.mapper.MallRoleMapper;
import com.cskaoyan.mapper.MallRolePermissionMapper;
import com.cskaoyan.service.RoleService;
import com.cskaoyan.util.MyPageHelper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Fengyi
 * @description: 角色管理实现类
 * @time: 2021/7/9 8:46
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    MallRoleMapper mallRoleMapper;

    @Autowired
    MallPermissionMapper mallPermissionMapper;

    @Autowired
    MallRolePermissionMapper mallRolePermissionMapper;

    @Autowired
    MallAdminMapper mallAdminMapper;

    @Override
    public List<SystemRoleVo> queryRoles() {

        //逻辑查询
        return mallRoleMapper.selectBySystemRoleVo();
    }

    @Override
    public BaseData<MallRole> queryAllRole(RoleParam param) {
        MyPageHelper.startPage(param);

        MallRoleExample mallRoleExample = new MallRoleExample();
        MallRoleExample.Criteria criteria = mallRoleExample.createCriteria();
        //逻辑删除
        criteria.andDeletedEqualTo(false);
        if (param.getName() != null) {
            criteria.andNameLike("%" + param.getName() + "%");
        }

        mallRoleExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallRole> roles = mallRoleMapper.selectByExample(mallRoleExample);
        long total = mallRoleMapper.countByExample(mallRoleExample);

        return BaseData.ok(roles, total);
    }

    /**
     * @Author fengyi
     * @description: 创建 role
     * @Date 14:38 2021/7/9
     * url:
     */
    @Override
    public int addRole(MallRole mallRole) {
        MallRoleExample mallRoleExample = new MallRoleExample();

        MallRoleExample.Criteria criteria = mallRoleExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andNameEqualTo(mallRole.getName());
        List<MallRole> mallRoles = mallRoleMapper.selectByExample(mallRoleExample);
        if (mallRoles.size() != 0) {
            //有重名
            return -1;
        }

        mallRoleMapper.insertSelective(mallRole);
        return mallRole.getId();
    }

    @Override
    public int updateRole(MallRole mallRole) {
        MallRoleExample mallRoleExample = new MallRoleExample();

        MallRoleExample.Criteria criteria = mallRoleExample.createCriteria();

        String oldName = mallRoleMapper.selectOldNameById(mallRole.getId());

        criteria.andNameNotEqualTo(oldName);
        criteria.andNameEqualTo(mallRole.getName());
        List<MallRole> mallRoles = mallRoleMapper.selectByExample(mallRoleExample);
        if (mallRoles.size() != 0) {
            //有重名
            return -1;
        }
        mallRoleMapper.updateByPrimaryKeySelective(mallRole);
        return 200;
    }


    @Override
    public Integer deleteRole(MallRole mallRole) {
        MallAdminExample example = new MallAdminExample();
        MallAdminExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MallAdmin> mallAdmins = mallAdminMapper.selectByExample(example);
        for (MallAdmin mallAdmin : mallAdmins) {
            Integer[] roleIds = mallAdmin.getRoleIds();
            for (Integer roleId : roleIds) {
                if (mallRole.getId().equals(roleId)) {
                    return 0;  // 返回0，说明该角色被用户引用，不能够被删除
                }
            }
        }
        Integer status = mallRolePermissionMapper.deleteRoleByRoleId(mallRole.getId());
        mallRoleMapper.updateDeleted(mallRole.getId(), new Date());
        return status;
    }

    @Override
    public String getPermissions(Integer roleId) {

        List<String> permissions = mallRoleMapper.selectPermissionsById(roleId);

        if (permissions.contains("*")) {
            return "{\"errno\":0,\"data\":{\"systemPermissions\":[{\"id\":\"系统管理\",\"label\":\"系统管理\",\"children\":[{\"id\":\"管理员管理\",\"label\":\"管理员管理\",\"children\":[{\"id\":\"admin:admin:update\",\"label\":\"编辑\",\"api\":\"POST /admin/admin/update\"},{\"id\":\"admin:admin:list\",\"label\":\"查询\",\"api\":\"GET /admin/admin/list\"},{\"id\":\"admin:admin:read\",\"label\":\"详情\",\"api\":\"GET /admin/admin/read\"},{\"id\":\"admin:admin:delete\",\"label\":\"删除\",\"api\":\"POST /admin/admin/delete\"},{\"id\":\"admin:admin:create\",\"label\":\"添加\",\"api\":\"POST /admin/admin/create\"}]},{\"id\":\"角色管理\",\"label\":\"角色管理\",\"children\":[{\"id\":\"admin:role:permission:update\",\"label\":\"权限变更\",\"api\":\"POST /admin/role/permissions\"},{\"id\":\"admin:role:update\",\"label\":\"角色编辑\",\"api\":\"POST /admin/role/update\"},{\"id\":\"admin:role:list\",\"label\":\"角色查询\",\"api\":\"GET /admin/role/list\"},{\"id\":\"admin:role:read\",\"label\":\"角色详情\",\"api\":\"GET /admin/role/read\"},{\"id\":\"admin:role:delete\",\"label\":\"角色删除\",\"api\":\"POST /admin/role/delete\"},{\"id\":\"admin:role:permission:get\",\"label\":\"权限详情\",\"api\":\"GET /admin/role/permissions\"},{\"id\":\"admin:role:create\",\"label\":\"角色添加\",\"api\":\"POST /admin/role/create\"}]},{\"id\":\"对象存储\",\"label\":\"对象存储\",\"children\":[{\"id\":\"admin:storage:update\",\"label\":\"编辑\",\"api\":\"POST /admin/storage/update\"},{\"id\":\"admin:storage:list\",\"label\":\"查询\",\"api\":\"GET /admin/storage/list\"},{\"id\":\"admin:storage:read\",\"label\":\"详情\",\"api\":\"POST /admin/storage/read\"},{\"id\":\"admin:storage:delete\",\"label\":\"删除\",\"api\":\"POST /admin/storage/delete\"},{\"id\":\"admin:storage:create\",\"label\":\"上传\",\"api\":\"POST /admin/storage/create\"}]},{\"id\":\"操作日志\",\"label\":\"操作日志\",\"children\":[{\"id\":\"admin:log:list\",\"label\":\"查询\",\"api\":\"GET /admin/log/list\"}]}]},{\"id\":\"商场管理\",\"label\":\"商场管理\",\"children\":[{\"id\":\"品牌管理\",\"label\":\"品牌管理\",\"children\":[{\"id\":\"admin:brand:update\",\"label\":\"编辑\",\"api\":\"POST /admin/brand/update\"},{\"id\":\"admin:brand:list\",\"label\":\"查询\",\"api\":\"GET /admin/brand/list\"},{\"id\":\"admin:brand:read\",\"label\":\"详情\",\"api\":\"GET /admin/brand/read\"},{\"id\":\"admin:brand:delete\",\"label\":\"删除\",\"api\":\"POST /admin/brand/delete\"},{\"id\":\"admin:brand:create\",\"label\":\"添加\",\"api\":\"POST /admin/brand/create\"}]},{\"id\":\"通用问题\",\"label\":\"通用问题\",\"children\":[{\"id\":\"admin:issue:update\",\"label\":\"编辑\",\"api\":\"POST /admin/issue/update\"},{\"id\":\"admin:issue:list\",\"label\":\"查询\",\"api\":\"GET /admin/issue/list\"},{\"id\":\"admin:issue:delete\",\"label\":\"删除\",\"api\":\"POST /admin/issue/delete\"},{\"id\":\"admin:issue:create\",\"label\":\"添加\",\"api\":\"POST /admin/issue/create\"}]},{\"id\":\"订单管理\",\"label\":\"订单管理\",\"children\":[{\"id\":\"admin:order:read\",\"label\":\"详情\",\"api\":\"GET /admin/order/detail\"},{\"id\":\"admin:order:ship\",\"label\":\"订单发货\",\"api\":\"POST /admin/order/ship\"},{\"id\":\"admin:order:refund\",\"label\":\"订单退款\",\"api\":\"POST /admin/order/refund\"},{\"id\":\"admin:order:reply\",\"label\":\"订单商品回复\",\"api\":\"POST /admin/order/reply\"},{\"id\":\"admin:order:list\",\"label\":\"查询\",\"api\":\"GET /admin/order/list\"}]},{\"id\":\"类目管理\",\"label\":\"类目管理\",\"children\":[{\"id\":\"admin:category:update\",\"label\":\"编辑\",\"api\":\"POST /admin/category/update\"},{\"id\":\"admin:category:list\",\"label\":\"查询\",\"api\":\"GET /admin/category/list\"},{\"id\":\"admin:category:read\",\"label\":\"详情\",\"api\":\"GET /admin/category/read\"},{\"id\":\"admin:category:delete\",\"label\":\"删除\",\"api\":\"POST /admin/category/delete\"},{\"id\":\"admin:category:create\",\"label\":\"添加\",\"api\":\"POST /admin/category/create\"}]},{\"id\":\"关键词\",\"label\":\"关键词\",\"children\":[{\"id\":\"admin:keyword:update\",\"label\":\"编辑\",\"api\":\"POST /admin/keyword/update\"},{\"id\":\"admin:keyword:list\",\"label\":\"查询\",\"api\":\"GET /admin/keyword/list\"},{\"id\":\"admin:keyword:read\",\"label\":\"详情\",\"api\":\"GET /admin/keyword/read\"},{\"id\":\"admin:keyword:delete\",\"label\":\"删除\",\"api\":\"POST /admin/keyword/delete\"},{\"id\":\"admin:keyword:create\",\"label\":\"添加\",\"api\":\"POST /admin/keyword/create\"}]}]},{\"id\":\"用户管理\",\"label\":\"用户管理\",\"children\":[{\"id\":\"用户收藏\",\"label\":\"用户收藏\",\"children\":[{\"id\":\"admin:collect:list\",\"label\":\"查询\",\"api\":\"GET /admin/collect/list\"}]},{\"id\":\"用户足迹\",\"label\":\"用户足迹\",\"children\":[{\"id\":\"admin:footprint:list\",\"label\":\"查询\",\"api\":\"GET /admin/footprint/list\"}]},{\"id\":\"意见反馈\",\"label\":\"意见反馈\",\"children\":[{\"id\":\"admin:feedback:list\",\"label\":\"查询\",\"api\":\"GET /admin/feedback/list\"}]},{\"id\":\"搜索历史\",\"label\":\"搜索历史\",\"children\":[{\"id\":\"admin:history:list\",\"label\":\"查询\",\"api\":\"GET /admin/history/list\"}]},{\"id\":\"会员管理\",\"label\":\"会员管理\",\"children\":[{\"id\":\"admin:user:list\",\"label\":\"查询\",\"api\":\"GET /admin/user/list\"}]},{\"id\":\"收货地址\",\"label\":\"收货地址\",\"children\":[{\"id\":\"admin:address:list\",\"label\":\"查询\",\"api\":\"GET /admin/address/list\"}]}]},{\"id\":\"配置管理\",\"label\":\"配置管理\",\"children\":[{\"id\":\"小程序配置\",\"label\":\"小程序配置\",\"children\":[{\"id\":\"admin:config:wx:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/wx\"},{\"id\":\"admin:config:wx:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/wx\"}]},{\"id\":\"运费配置\",\"label\":\"运费配置\",\"children\":[{\"id\":\"admin:config:express:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/express\"},{\"id\":\"admin:config:express:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/express\"}]},{\"id\":\"商场配置\",\"label\":\"商场配置\",\"children\":[{\"id\":\"admin:config:mall:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/mall\"},{\"id\":\"admin:config:mall:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/mall\"}]},{\"id\":\"订单配置\",\"label\":\"订单配置\",\"children\":[{\"id\":\"admin:config:order:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/order\"},{\"id\":\"admin:config:order:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/order\"}]}]},{\"id\":\"推广管理\",\"label\":\"推广管理\",\"children\":[{\"id\":\"广告管理\",\"label\":\"广告管理\",\"children\":[{\"id\":\"admin:ad:update\",\"label\":\"编辑\",\"api\":\"POST /admin/ad/update\"},{\"id\":\"admin:ad:list\",\"label\":\"查询\",\"api\":\"GET /admin/ad/list\"},{\"id\":\"admin:ad:read\",\"label\":\"详情\",\"api\":\"GET /admin/ad/read\"},{\"id\":\"admin:ad:delete\",\"label\":\"删除\",\"api\":\"POST /admin/ad/delete\"},{\"id\":\"admin:ad:create\",\"label\":\"添加\",\"api\":\"POST /admin/ad/create\"}]},{\"id\":\"专题管理\",\"label\":\"专题管理\",\"children\":[{\"id\":\"admin:topic:update\",\"label\":\"编辑\",\"api\":\"POST /admin/topic/update\"},{\"id\":\"admin:topic:list\",\"label\":\"查询\",\"api\":\"GET /admin/topic/list\"},{\"id\":\"admin:topic:read\",\"label\":\"详情\",\"api\":\"GET /admin/topic/read\"},{\"id\":\"admin:topic:delete\",\"label\":\"删除\",\"api\":\"POST /admin/topic/delete\"},{\"id\":\"admin:topic:create\",\"label\":\"添加\",\"api\":\"POST /admin/topic/create\"}]},{\"id\":\"优惠券管理\",\"label\":\"优惠券管理\",\"children\":[{\"id\":\"admin:coupon:listuser\",\"label\":\"查询用户\",\"api\":\"GET /admin/coupon/listuser\"},{\"id\":\"admin:coupon:update\",\"label\":\"编辑\",\"api\":\"POST /admin/coupon/update\"},{\"id\":\"admin:coupon:list\",\"label\":\"查询\",\"api\":\"GET /admin/coupon/list\"},{\"id\":\"admin:coupon:read\",\"label\":\"详情\",\"api\":\"GET /admin/coupon/read\"},{\"id\":\"admin:coupon:delete\",\"label\":\"删除\",\"api\":\"POST /admin/coupon/delete\"},{\"id\":\"admin:coupon:create\",\"label\":\"添加\",\"api\":\"POST /admin/coupon/create\"}]},{\"id\":\"团购管理\",\"label\":\"团购管理\",\"children\":[{\"id\":\"admin:groupon:read\",\"label\":\"详情\",\"api\":\"GET /admin/groupon/listRecord\"},{\"id\":\"admin:groupon:update\",\"label\":\"编辑\",\"api\":\"POST /admin/groupon/update\"},{\"id\":\"admin:groupon:list\",\"label\":\"查询\",\"api\":\"GET /admin/groupon/list\"},{\"id\":\"admin:groupon:delete\",\"label\":\"删除\",\"api\":\"POST /admin/groupon/delete\"},{\"id\":\"admin:groupon:create\",\"label\":\"添加\",\"api\":\"POST /admin/groupon/create\"}]}]},{\"id\":\"商品管理\",\"label\":\"商品管理\",\"children\":[{\"id\":\"商品管理\",\"label\":\"商品管理\",\"children\":[{\"id\":\"admin:goods:read\",\"label\":\"详情\",\"api\":\"GET /admin/goods/detail\"},{\"id\":\"admin:goods:update\",\"label\":\"编辑\",\"api\":\"POST /admin/goods/update\"},{\"id\":\"admin:goods:list\",\"label\":\"查询\",\"api\":\"GET /admin/goods/list\"},{\"id\":\"admin:goods:delete\",\"label\":\"删除\",\"api\":\"POST /admin/goods/delete\"},{\"id\":\"admin:goods:create\",\"label\":\"上架\",\"api\":\"POST /admin/goods/create\"}]},{\"id\":\"评论管理\",\"label\":\"评论管理\",\"children\":[{\"id\":\"admin:comment:list\",\"label\":\"查询\",\"api\":\"GET /admin/comment/list\"},{\"id\":\"admin:comment:delete\",\"label\":\"删除\",\"api\":\"POST /admin/comment/delete\"}]}]},{\"id\":\"其他\",\"label\":\"其他\",\"children\":[{\"id\":\"权限测试\",\"label\":\"权限测试\",\"children\":[{\"id\":\"index:permission:write\",\"label\":\"权限写\",\"api\":\"POST /admin/index/write\"},{\"id\":\"index:permission:read\",\"label\":\"权限读\",\"api\":\"GET /admin/index/read\"}]}]},{\"id\":\"统计管理\",\"label\":\"统计管理\",\"children\":[{\"id\":\"用户统计\",\"label\":\"用户统计\",\"children\":[{\"id\":\"admin:stat:user\",\"label\":\"查询\",\"api\":\"GET /admin/stat/user\"}]},{\"id\":\"订单统计\",\"label\":\"订单统计\",\"children\":[{\"id\":\"admin:stat:order\",\"label\":\"查询\",\"api\":\"GET /admin/stat/order\"}]},{\"id\":\"商品统计\",\"label\":\"商品统计\",\"children\":[{\"id\":\"admin:stat:goods\",\"label\":\"查询\",\"api\":\"GET /admin/stat/goods\"}]}]}],\"assignedPermissions\":[\"admin:order:list\",\"admin:admin:update\",\"admin:topic:read\",\"admin:coupon:delete\",\"admin:admin:delete\",\"admin:goods:update\",\"admin:user:list\",\"admin:role:permission:get\",\"admin:brand:update\",\"admin:category:create\",\"admin:ad:create\",\"admin:coupon:list\",\"admin:stat:order\",\"admin:config:wx:updateConfigs\",\"admin:topic:list\",\"admin:order:refund\",\"admin:order:read\",\"admin:topic:delete\",\"admin:brand:list\",\"admin:brand:delete\",\"admin:coupon:update\",\"admin:brand:read\",\"admin:config:wx:list\",\"admin:collect:list\",\"admin:storage:list\",\"admin:coupon:listuser\",\"admin:groupon:read\",\"admin:admin:read\",\"admin:order:ship\",\"admin:storage:read\",\"admin:keyword:update\",\"admin:comment:delete\",\"admin:groupon:create\",\"admin:comment:list\",\"admin:keyword:list\",\"admin:keyword:create\",\"admin:admin:list\",\"admin:category:delete\",\"admin:history:list\",\"admin:role:delete\",\"admin:storage:delete\",\"admin:order:reply\",\"admin:keyword:read\",\"admin:ad:delete\",\"admin:goods:delete\",\"admin:issue:update\",\"admin:topic:create\",\"admin:address:list\",\"admin:category:read\",\"admin:category:update\",\"admin:config:express:updateConfigs\",\"admin:storage:create\",\"admin:brand:create\",\"admin:config:express:list\",\"admin:issue:delete\",\"admin:goods:create\",\"admin:ad:list\",\"admin:role:permission:update\",\"admin:groupon:list\",\"admin:admin:create\",\"admin:groupon:update\",\"admin:footprint:list\",\"index:permission:write\",\"admin:ad:read\",\"admin:groupon:delete\",\"admin:config:order:list\",\"index:permission:read\",\"admin:keyword:delete\",\"admin:role:create\",\"admin:config:order:updateConfigs\",\"admin:issue:list\",\"admin:log:list\",\"admin:topic:update\",\"admin:config:mall:list\",\"admin:category:list\",\"admin:stat:goods\",\"admin:issue:create\",\"admin:config:mall:updateConfigs\",\"admin:role:update\",\"admin:stat:user\",\"admin:goods:list\",\"admin:coupon:read\",\"admin:coupon:create\",\"admin:ad:update\",\"admin:role:list\",\"admin:storage:update\",\"admin:role:read\",\"admin:feedback:list\",\"admin:goods:read\"]},\"errmsg\":\"成功\"}";
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String permissionsStr = null;
        try {
            permissionsStr = objectMapper.writeValueAsString(permissions);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }


        String str1 = "{\"errno\":0,\"data\":{\"systemPermissions\":[{\"id\":\"系统管理\",\"label\":\"系统管理\",\"children\":[{\"id\":\"管理员管理\",\"label\":\"管理员管理\",\"children\":[{\"id\":\"admin:admin:update\",\"label\":\"编辑\",\"api\":\"POST /admin/admin/update\"},{\"id\":\"admin:admin:list\",\"label\":\"查询\",\"api\":\"GET /admin/admin/list\"},{\"id\":\"admin:admin:read\",\"label\":\"详情\",\"api\":\"GET /admin/admin/read\"},{\"id\":\"admin:admin:delete\",\"label\":\"删除\",\"api\":\"POST /admin/admin/delete\"},{\"id\":\"admin:admin:create\",\"label\":\"添加\",\"api\":\"POST /admin/admin/create\"}]},{\"id\":\"角色管理\",\"label\":\"角色管理\",\"children\":[{\"id\":\"admin:role:permission:update\",\"label\":\"权限变更\",\"api\":\"POST /admin/role/permissions\"},{\"id\":\"admin:role:update\",\"label\":\"角色编辑\",\"api\":\"POST /admin/role/update\"},{\"id\":\"admin:role:list\",\"label\":\"角色查询\",\"api\":\"GET /admin/role/list\"},{\"id\":\"admin:role:read\",\"label\":\"角色详情\",\"api\":\"GET /admin/role/read\"},{\"id\":\"admin:role:delete\",\"label\":\"角色删除\",\"api\":\"POST /admin/role/delete\"},{\"id\":\"admin:role:permission:get\",\"label\":\"权限详情\",\"api\":\"GET /admin/role/permissions\"},{\"id\":\"admin:role:create\",\"label\":\"角色添加\",\"api\":\"POST /admin/role/create\"}]},{\"id\":\"对象存储\",\"label\":\"对象存储\",\"children\":[{\"id\":\"admin:storage:update\",\"label\":\"编辑\",\"api\":\"POST /admin/storage/update\"},{\"id\":\"admin:storage:list\",\"label\":\"查询\",\"api\":\"GET /admin/storage/list\"},{\"id\":\"admin:storage:read\",\"label\":\"详情\",\"api\":\"POST /admin/storage/read\"},{\"id\":\"admin:storage:delete\",\"label\":\"删除\",\"api\":\"POST /admin/storage/delete\"},{\"id\":\"admin:storage:create\",\"label\":\"上传\",\"api\":\"POST /admin/storage/create\"}]},{\"id\":\"操作日志\",\"label\":\"操作日志\",\"children\":[{\"id\":\"admin:log:list\",\"label\":\"查询\",\"api\":\"GET /admin/log/list\"}]}]},{\"id\":\"商场管理\",\"label\":\"商场管理\",\"children\":[{\"id\":\"品牌管理\",\"label\":\"品牌管理\",\"children\":[{\"id\":\"admin:brand:update\",\"label\":\"编辑\",\"api\":\"POST /admin/brand/update\"},{\"id\":\"admin:brand:list\",\"label\":\"查询\",\"api\":\"GET /admin/brand/list\"},{\"id\":\"admin:brand:read\",\"label\":\"详情\",\"api\":\"GET /admin/brand/read\"},{\"id\":\"admin:brand:delete\",\"label\":\"删除\",\"api\":\"POST /admin/brand/delete\"},{\"id\":\"admin:brand:create\",\"label\":\"添加\",\"api\":\"POST /admin/brand/create\"}]},{\"id\":\"通用问题\",\"label\":\"通用问题\",\"children\":[{\"id\":\"admin:issue:update\",\"label\":\"编辑\",\"api\":\"POST /admin/issue/update\"},{\"id\":\"admin:issue:list\",\"label\":\"查询\",\"api\":\"GET /admin/issue/list\"},{\"id\":\"admin:issue:delete\",\"label\":\"删除\",\"api\":\"POST /admin/issue/delete\"},{\"id\":\"admin:issue:create\",\"label\":\"添加\",\"api\":\"POST /admin/issue/create\"}]},{\"id\":\"订单管理\",\"label\":\"订单管理\",\"children\":[{\"id\":\"admin:order:read\",\"label\":\"详情\",\"api\":\"GET /admin/order/detail\"},{\"id\":\"admin:order:ship\",\"label\":\"订单发货\",\"api\":\"POST /admin/order/ship\"},{\"id\":\"admin:order:refund\",\"label\":\"订单退款\",\"api\":\"POST /admin/order/refund\"},{\"id\":\"admin:order:reply\",\"label\":\"订单商品回复\",\"api\":\"POST /admin/order/reply\"},{\"id\":\"admin:order:list\",\"label\":\"查询\",\"api\":\"GET /admin/order/list\"}]},{\"id\":\"类目管理\",\"label\":\"类目管理\",\"children\":[{\"id\":\"admin:category:update\",\"label\":\"编辑\",\"api\":\"POST /admin/category/update\"},{\"id\":\"admin:category:list\",\"label\":\"查询\",\"api\":\"GET /admin/category/list\"},{\"id\":\"admin:category:read\",\"label\":\"详情\",\"api\":\"GET /admin/category/read\"},{\"id\":\"admin:category:delete\",\"label\":\"删除\",\"api\":\"POST /admin/category/delete\"},{\"id\":\"admin:category:create\",\"label\":\"添加\",\"api\":\"POST /admin/category/create\"}]},{\"id\":\"关键词\",\"label\":\"关键词\",\"children\":[{\"id\":\"admin:keyword:update\",\"label\":\"编辑\",\"api\":\"POST /admin/keyword/update\"},{\"id\":\"admin:keyword:list\",\"label\":\"查询\",\"api\":\"GET /admin/keyword/list\"},{\"id\":\"admin:keyword:read\",\"label\":\"详情\",\"api\":\"GET /admin/keyword/read\"},{\"id\":\"admin:keyword:delete\",\"label\":\"删除\",\"api\":\"POST /admin/keyword/delete\"},{\"id\":\"admin:keyword:create\",\"label\":\"添加\",\"api\":\"POST /admin/keyword/create\"}]}]},{\"id\":\"用户管理\",\"label\":\"用户管理\",\"children\":[{\"id\":\"用户收藏\",\"label\":\"用户收藏\",\"children\":[{\"id\":\"admin:collect:list\",\"label\":\"查询\",\"api\":\"GET /admin/collect/list\"}]},{\"id\":\"用户足迹\",\"label\":\"用户足迹\",\"children\":[{\"id\":\"admin:footprint:list\",\"label\":\"查询\",\"api\":\"GET /admin/footprint/list\"}]},{\"id\":\"意见反馈\",\"label\":\"意见反馈\",\"children\":[{\"id\":\"admin:feedback:list\",\"label\":\"查询\",\"api\":\"GET /admin/feedback/list\"}]},{\"id\":\"搜索历史\",\"label\":\"搜索历史\",\"children\":[{\"id\":\"admin:history:list\",\"label\":\"查询\",\"api\":\"GET /admin/history/list\"}]},{\"id\":\"会员管理\",\"label\":\"会员管理\",\"children\":[{\"id\":\"admin:user:list\",\"label\":\"查询\",\"api\":\"GET /admin/user/list\"}]},{\"id\":\"收货地址\",\"label\":\"收货地址\",\"children\":[{\"id\":\"admin:address:list\",\"label\":\"查询\",\"api\":\"GET /admin/address/list\"}]}]},{\"id\":\"配置管理\",\"label\":\"配置管理\",\"children\":[{\"id\":\"小程序配置\",\"label\":\"小程序配置\",\"children\":[{\"id\":\"admin:config:wx:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/wx\"},{\"id\":\"admin:config:wx:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/wx\"}]},{\"id\":\"运费配置\",\"label\":\"运费配置\",\"children\":[{\"id\":\"admin:config:express:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/express\"},{\"id\":\"admin:config:express:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/express\"}]},{\"id\":\"商场配置\",\"label\":\"商场配置\",\"children\":[{\"id\":\"admin:config:mall:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/mall\"},{\"id\":\"admin:config:mall:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/mall\"}]},{\"id\":\"订单配置\",\"label\":\"订单配置\",\"children\":[{\"id\":\"admin:config:order:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/order\"},{\"id\":\"admin:config:order:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/order\"}]}]},{\"id\":\"推广管理\",\"label\":\"推广管理\",\"children\":[{\"id\":\"广告管理\",\"label\":\"广告管理\",\"children\":[{\"id\":\"admin:ad:update\",\"label\":\"编辑\",\"api\":\"POST /admin/ad/update\"},{\"id\":\"admin:ad:list\",\"label\":\"查询\",\"api\":\"GET /admin/ad/list\"},{\"id\":\"admin:ad:read\",\"label\":\"详情\",\"api\":\"GET /admin/ad/read\"},{\"id\":\"admin:ad:delete\",\"label\":\"删除\",\"api\":\"POST /admin/ad/delete\"},{\"id\":\"admin:ad:create\",\"label\":\"添加\",\"api\":\"POST /admin/ad/create\"}]},{\"id\":\"专题管理\",\"label\":\"专题管理\",\"children\":[{\"id\":\"admin:topic:update\",\"label\":\"编辑\",\"api\":\"POST /admin/topic/update\"},{\"id\":\"admin:topic:list\",\"label\":\"查询\",\"api\":\"GET /admin/topic/list\"},{\"id\":\"admin:topic:read\",\"label\":\"详情\",\"api\":\"GET /admin/topic/read\"},{\"id\":\"admin:topic:delete\",\"label\":\"删除\",\"api\":\"POST /admin/topic/delete\"},{\"id\":\"admin:topic:create\",\"label\":\"添加\",\"api\":\"POST /admin/topic/create\"}]},{\"id\":\"优惠券管理\",\"label\":\"优惠券管理\",\"children\":[{\"id\":\"admin:coupon:listuser\",\"label\":\"查询用户\",\"api\":\"GET /admin/coupon/listuser\"},{\"id\":\"admin:coupon:update\",\"label\":\"编辑\",\"api\":\"POST /admin/coupon/update\"},{\"id\":\"admin:coupon:list\",\"label\":\"查询\",\"api\":\"GET /admin/coupon/list\"},{\"id\":\"admin:coupon:read\",\"label\":\"详情\",\"api\":\"GET /admin/coupon/read\"},{\"id\":\"admin:coupon:delete\",\"label\":\"删除\",\"api\":\"POST /admin/coupon/delete\"},{\"id\":\"admin:coupon:create\",\"label\":\"添加\",\"api\":\"POST /admin/coupon/create\"}]},{\"id\":\"团购管理\",\"label\":\"团购管理\",\"children\":[{\"id\":\"admin:groupon:read\",\"label\":\"详情\",\"api\":\"GET /admin/groupon/listRecord\"},{\"id\":\"admin:groupon:update\",\"label\":\"编辑\",\"api\":\"POST /admin/groupon/update\"},{\"id\":\"admin:groupon:list\",\"label\":\"查询\",\"api\":\"GET /admin/groupon/list\"},{\"id\":\"admin:groupon:delete\",\"label\":\"删除\",\"api\":\"POST /admin/groupon/delete\"},{\"id\":\"admin:groupon:create\",\"label\":\"添加\",\"api\":\"POST /admin/groupon/create\"}]}]},{\"id\":\"商品管理\",\"label\":\"商品管理\",\"children\":[{\"id\":\"商品管理\",\"label\":\"商品管理\",\"children\":[{\"id\":\"admin:goods:read\",\"label\":\"详情\",\"api\":\"GET /admin/goods/detail\"},{\"id\":\"admin:goods:update\",\"label\":\"编辑\",\"api\":\"POST /admin/goods/update\"},{\"id\":\"admin:goods:list\",\"label\":\"查询\",\"api\":\"GET /admin/goods/list\"},{\"id\":\"admin:goods:delete\",\"label\":\"删除\",\"api\":\"POST /admin/goods/delete\"},{\"id\":\"admin:goods:create\",\"label\":\"上架\",\"api\":\"POST /admin/goods/create\"}]},{\"id\":\"评论管理\",\"label\":\"评论管理\",\"children\":[{\"id\":\"admin:comment:list\",\"label\":\"查询\",\"api\":\"GET /admin/comment/list\"},{\"id\":\"admin:comment:delete\",\"label\":\"删除\",\"api\":\"POST /admin/comment/delete\"}]}]},{\"id\":\"其他\",\"label\":\"其他\",\"children\":[{\"id\":\"权限测试\",\"label\":\"权限测试\",\"children\":[{\"id\":\"index:permission:write\",\"label\":\"权限写\",\"api\":\"POST /admin/index/write\"},{\"id\":\"index:permission:read\",\"label\":\"权限读\",\"api\":\"GET /admin/index/read\"}]}]},{\"id\":\"统计管理\",\"label\":\"统计管理\",\"children\":[{\"id\":\"用户统计\",\"label\":\"用户统计\",\"children\":[{\"id\":\"admin:stat:user\",\"label\":\"查询\",\"api\":\"GET /admin/stat/user\"}]},{\"id\":\"订单统计\",\"label\":\"订单统计\",\"children\":[{\"id\":\"admin:stat:order\",\"label\":\"查询\",\"api\":\"GET /admin/stat/order\"}]},{\"id\":\"商品统计\",\"label\":\"商品统计\",\"children\":[{\"id\":\"admin:stat:goods\",\"label\":\"查询\",\"api\":\"GET /admin/stat/goods\"}]}]}],\"assignedPermissions\":";
        String str2 = "},\"errmsg\":\"成功\"}";
        return str1 + permissionsStr + str2;
    }

    @Override
    public void updatePermissions(PermissionVO permissions) {


        //将当前role的所有permission 逻辑删除
        mallRoleMapper.updateAllDeletedByRoleId(permissions.getRoleId());



        for (String permission : permissions.getPermissions()) {

            MallPermissionExample mallPermissionExample = new MallPermissionExample();

            MallPermission mallPermission = new MallPermission();
            mallPermission.setRoleId(permissions.getRoleId());
            mallPermission.setPermission(permission);
            Date date = new Date();

            MallPermissionExample.Criteria criteria = mallPermissionExample.createCriteria();
            criteria.andRoleIdEqualTo(permissions.getRoleId());

            //判断新添加的permission是否存在
            int count = mallRoleMapper.selectByPermission(permission, permissions.getRoleId());
            if (count == 0) {
                //如果不存在，则insert，更新addTime， updatetime

                mallPermission.setAddTime(date);
                mallPermission.setUpdateTime(date);

                mallPermissionMapper.insertSelective(mallPermission);

            } else {
                //如果存在，则修改deleted=1，更新updateTime

                criteria.andPermissionEqualTo(permission);
                mallPermission.setUpdateTime(date);
                mallPermission.setDeleted(false);

                mallPermissionMapper.updateByExampleSelective(mallPermission, mallPermissionExample);
            }
        }
    }

    /**
     * @Author harvey
     * @description: 该方法主要是为了来满足前端登录进来之后调用info方法时需要的参数
     *              根据一些permission的规律来进行实现这个功能
     * @Date 2021/7/12 16:54
     * url:
     */
    @Override
    public Integer updateOrInsertRolePermissions(PermissionVO permissionVO) {
        String[] permissions = permissionVO.getPermissions();
        List<String> permissionsList = new ArrayList<>();
        for (String permission : permissions) {
            permissionsList.add(permission);
        }
        for (int i = 0; i < permissionsList.size(); i++) {
            if (permissionsList.contains("admin:config:wx:list")) {
                permissionsList.remove("admin:config:wx:list");
                permissionsList.add("GET /admin/config/wx");
            } else if (permissionsList.contains("admin:config:wx:updateConfigs")) {
                permissionsList.remove("admin:config:wx:updateConfigs");
                permissionsList.add("POST /admin/config/wx");
            } else if (permissionsList.contains("admin:config:express:list")) {
                permissionsList.remove("admin:config:express:list");
                permissionsList.add("GET /admin/config/express");
            } else if (permissionsList.contains("admin:config:express:updateConfigs")) {
                permissionsList.remove("admin:config:express:updateConfigs");
                permissionsList.add("POST /admin/config/express");
            } else if (permissionsList.contains("admin:config:mall:list")) {
                permissionsList.remove("admin:config:mall:list");
                permissionsList.add("GET /admin/config/mall");
            } else if (permissionsList.contains("admin:config:mall:updateConfigs")) {
                permissionsList.remove("admin:config:mall:updateConfigs");
                permissionsList.add("POST /admin/config/mall");
            } else if (permissionsList.contains("admin:config:order:list")) {
                permissionsList.remove("admin:config:order:list");
                permissionsList.add("GET /admin/config/order");
            } else if (permissionsList.contains("admin:config:order:updateConfigs")) {
                permissionsList.remove("admin:config:order:updateConfigs");
                permissionsList.add("POST /admin/config/order");
            } else if (permissionsList.contains("admin:role:permission:update")) {
                permissionsList.remove("admin:role:permission:update");
                permissionsList.add("POST /admin/role/permissions");
            } else if (permissionsList.contains("admin:role:permission:get")) {
                permissionsList.remove("admin:role:permission:get");
                permissionsList.add("GET /admin/role/permissions");
            } else if (permissionsList.contains("admin:storage:read")) {
                //这本来要是get的方式  所以我们之后的代码中还要进行特殊的判断
                permissionsList.remove("admin:storage:read");
                permissionsList.add("POST /admin/storage/read");
            } else if (permissionsList.contains("admin:groupon:read")) {
                permissionsList.remove("admin:groupon:read");
                permissionsList.add("GET /admin/groupon/listRecord");
            } else if (permissionsList.contains("admin:goods:read")) {
                permissionsList.remove("admin:goods:read");
                permissionsList.add("GET /admin/goods/detail");
            } else if (permissionsList.contains("admin:order:read")) {
                permissionsList.remove("admin:order:read");
                permissionsList.add("GET /admin/order/detail");
            } else if (permissionsList.contains("index:permission:write")) {
                permissionsList.remove("index:permission:write");
                permissionsList.add("POST /admin/index/write");
            } else if (permissionsList.contains("index:permission:read")) {
                //跟前面的一样，在info中需要来继续进行一下判断
                permissionsList.remove("index:permission:read");
                permissionsList.add("GET /admin/index/read");
            } else if (permissionsList.contains("admin:stat:user")) {
                permissionsList.remove("admin:stat:user");
                permissionsList.add("GET /admin/stat/user");
            } else if (permissionsList.contains("admin:stat:order")) {
                permissionsList.remove("admin:stat:order");
                permissionsList.add("GET /admin/stat/order");
            } else if (permissionsList.contains("admin:stat:goods")) {
                permissionsList.remove("admin:stat:goods");
                permissionsList.add("GET /admin/stat/goods");
            }
        }


        /*for (String permission : permissions) {
            if (permission.equals("admin:config:wx:list")) {
                permissionsList.add("GET /admin/config/wx");
            } else if (permission.equals("admin:config:wx:updateConfigs")) {
                permissionsList.add("POST /admin/config/wx");
            } else if (permission.equals("admin:config:express:list")) {
                permissionsList.add("GET /admin/config/express");
            } else if (permission.equals("admin:config:express:updateConfigs")) {
                permissionsList.add("POST /admin/config/express");
            } else if (permission.equals("admin:config:mall:list")) {
                permissionsList.add("GET /admin/config/mall");
            } else if (permission.equals("admin:config:mall:updateConfigs")) {
                permissionsList.add("POST /admin/config/mall");
            } else if (permission.equals("admin:config:order:list")) {
                permissionsList.add("GET /admin/config/order");
            } else if (permission.equals("admin:config:order:updateConfigs")) {
                permissionsList.add("POST /admin/config/order");
            }
        }*/
        String[] permissions2 = new String[permissionsList.size()];
        for (int i = 0; i < permissions2.length; i++) {
            permissions2[i] = permissionsList.get(i);
        }
        permissionVO.setPermissions(permissions2);

        PermissionVO permissionVO1 = mallRolePermissionMapper.selectRolePermissionVOByRoleId(permissionVO.getRoleId());
        if (permissionVO1 == null) {
            Integer status = mallRolePermissionMapper.insertRolePermission(permissionVO);
            return status;
        } else {
            Integer status = mallRolePermissionMapper.updateRolePermissionByRoleId(permissionVO);
            return status;
        }
    }


}
